package com.chap1;

/**
 * <p>
 * <code>DeamonThread</code>
 * </p>
 *
 * @author junbao3
 * TODO
 * @date 2023-03-23 14:20
 * Copyright (C) 2021 IFlyTek. All rights reserved.
 */
public class DeamonThread {
    public static void main(String[] args) throws InterruptedException {
//        UseThread useThread = new UseThread();
//        useThread.start();
//        Thread.sleep(1000);
//        System.out.println("main end");//主线程结束，useThread还会继续执行


        UseThread useThread = new UseThread();
        useThread.setDaemon(true);
        useThread.start();
        Thread.sleep(10);
        System.out.println("main end");//主线程结束，useThread也结束
    }

    private static class UseThread extends Thread{
        @Override
        public void run() {
            try {
                while(true){
                    System.out.println(Thread.currentThread().getName()+" I am extends Thread");
                }
            }finally {
                //守护线程中finally不一定起作用
                System.out.println("finally");
            }
        }
    }
}
